package com.happystudy.controller;

import com.happpystudy.model.User;
import com.happpystudy.model.course.CourseTopic;
import com.happpystudy.service.CourseTopicService;
import com.happystudy.message.BaseMessageModel;
import com.happystudy.message.ErrorMessage;
import com.happystudy.message.ErrorMessageType;
import com.happystudy.message.SingleDataMessage;
import com.happystudy.util.KeyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CourseBordController {

    @Resource
    CourseTopicService courseTopicService;

    @GetMapping({"/courseBordTeacher","/courseBordStudent"})
    public String returnCourseBordHTML(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if (session==null){
            return "login";
        }

        User user = (User) session.getAttribute(KeyUtil.SESSION_CURRENT_USER_KEY);
        System.out.println(user);
        if (user==null){
            return "login";
        }
        return "courseBordTeacher";
    }

//    @GetMapping("/courseBordStudent")
//    public String returnCourseBordHTML(HttpServletRequest request){
//        HttpSession session = request.getSession(false);
//        if (session==null){
//            return "login";
//        }
//
//        User user = (User) session.getAttribute(KeyUtil.SESSION_CURRENT_USER_KEY);
//        if (user==null){
//            return "login";
//        }
//        return "courseBordTeacher";
//    }

    @GetMapping("/courseInfo/{uniId}/{courseId}")
    public String returnCourseInfo(HttpServletRequest request, Model model,
                                   @PathVariable(value = "uniId") String uniId,@PathVariable(value = "courseId") String courseId){
        HttpSession session = request.getSession(false);
        if (session==null){
            return "login";
        }
        User user = (User) session.getAttribute(KeyUtil.SESSION_CURRENT_USER_KEY);
        System.out.println(user);
        model.addAttribute("user",user);
        model.addAttribute("uniId",uniId);
        model.addAttribute("courseId",courseId);
        String role = user.getRole();
        if ("learner".equalsIgnoreCase(role)){
            return "courseItemTeacher";
        }else {
            return "courseItemTeacher";
        }

    }

    @GetMapping("/course/createCourseTopic/{uniId}/{courseId}/{courseGroup}")
    public String returnCreateCourseTopicHTML(Model model,
                                              @PathVariable(value = "uniId") String uniId,
                                              @PathVariable(value = "courseId") String courseId,
                                              @PathVariable(value = "courseGroup") Integer courseGroup){

        List<CourseTopic> courseTopics = courseTopicService.queryAllCourseTopicByCourseKey(uniId, courseId, courseGroup);
        System.out.println(courseTopics);
        model.addAttribute("courseTopics",courseTopics);
        model.addAttribute("backUrl","/courseInfo/"+uniId+"/"+courseId);
        model.addAttribute("courseId",courseId);
        model.addAttribute("courseGroup",courseGroup);
        return "creatCourseTopic";
    }


    @PostMapping("/course/createCourseTopic")
    @ResponseBody
    public BaseMessageModel createCourseTopic(CourseTopic courseTopic){
        String courseTopicName = courseTopic.getTopicName();
        if (courseTopicService.isCurrentTopicNameExist(courseTopicName)){
            return new ErrorMessage(ErrorMessageType.CURRENT_TOPICNAME_IS_EXIST_CODE,
                    ErrorMessageType.CURRENT_TOPICNAME_IS_EXIST_MSG);
        }
        System.out.println(courseTopic);
        courseTopic = courseTopicService.doCreateCourseTopic(courseTopic);
        if (courseTopic!=null){
            return new SingleDataMessage(courseTopic);
        }
        return new ErrorMessage(ErrorMessageType.INSERT_DATA_FAIL_CODE,
                ErrorMessageType.INSERT_DATA_FEIL_MSG);
    }
    @GetMapping("/createCourseItem/{topicId}/{uniId}/{courseId}/{courseGroup}")
    public String returnCreateCourseItemHTML(Model model,
                                             @PathVariable(value = "topicId") Integer topicId,
                                             @PathVariable(value = "uniId") String uniId,
                                             @PathVariable(value = "courseId") String courseId,
                                             @PathVariable(value = "courseGroup") Integer courseGroup){

        List<CourseTopic> courseTopics = courseTopicService.queryAllCourseTopicByCourseKey(uniId, courseId, courseGroup);
        CourseTopic courseTopic = courseTopicService.queryCourseTopicByParimaryKey(topicId);

        model.addAttribute("topics",courseTopics);
        model.addAttribute("currentTopic",courseTopic);
        model.addAttribute("uniId",uniId);
        model.addAttribute("courseId",courseId);
        model.addAttribute("courseGroup",courseGroup);
        return "createCourseItem";
    }

    @GetMapping("/courseBordTeacher/crades")
    public String returnGradesBord(){


        return "gradeBord";
    }

}
